package com.xyzk.diagnose.view.dialog;
import java.util.ArrayList;

import com.xyzk.diagnose.R;

import android.app.Activity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;


public class SelectDialog extends  BaseDialog{
	private TextView tvShow;
	private int position = -1;
	private ArrayList<String> data = new ArrayList<String>();
	private CPopupWindow popWin;

	public SelectDialog(Activity context,ArrayList<String> data) {
		super(context,R.id.tv_dialog_select);
		this.data = data;
		initView(context);
	}

	private void initView(final Activity context){
		tvShow = (TextView) super.initView();
		tvShow.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				popWin = new CPopupWindow(context, data);
				popWin.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 40);
				popWin.onClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
						if(!TextUtils.isEmpty(data.get(arg2))){
							setDefault(arg2);
							popWin.dismiss();
						}
					}
				});
			}
		});
	}


	/**
	 * 获取选中的位置
	 * @return 选中下标
	 */
	public int getSelectItem(){
		return position;
	}

	/**
	 * 设置默认选项
	 * @param position 下标
	 */
	public void setDefault(int position){
		if(position < data.size()){
			this.position = position;
			tvShow.setText(data.get(position));
		}else if(data.size() > 0){
			tvShow.setText(data.get(0));
		}
	}
	public void setDefault(String Pcar){
		for(int k = 0;k < data.size(); k ++)
		{
			if(Pcar.equals(data.get(k)))
			{
				tvShow.setText(Pcar);
				break;
			}
		}
	}
	/**
	 * 获取内容
	 * @return
	 */
	public String getContent()
	{
		String v_show = tvShow.getText().toString();
		return (data == null || data.size() == 0 || position >= data.size() || position < 0) ? v_show : data.get(position);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(popWin != null){
				popWin.dismiss();
				popWin = null;
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
